package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.entity.LawRegulationsStandardMes;
import com.ruoyi.system.entity.LawRegulationsStandardMesList;
import com.ruoyi.system.mapper.LawRegulationsStandardMesMapper;
import com.ruoyi.system.service.LawRegulationsStandardMesService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 法律、法规及标准(LawRegulationsStandardMes)表服务实现类
 *
 * @author makejava
 * @since 2023-09-14 13:38:50
 */
@Service("lawRegulationsStandardMesService")
public class LawRegulationsStandardMesServiceImpl implements LawRegulationsStandardMesService {
    @Resource
    private LawRegulationsStandardMesMapper lawRegulationsStandardMesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LawRegulationsStandardMes queryById(Integer id) {
        return this.lawRegulationsStandardMesDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param lawRegulationsStandardMes 筛选条件
     * @param pageRequest               分页对象
     * @return 查询结果
     */
    @Override
    public Page<LawRegulationsStandardMes> queryByPage(LawRegulationsStandardMes lawRegulationsStandardMes, PageRequest pageRequest) {
        long total = this.lawRegulationsStandardMesDao.count(lawRegulationsStandardMes);
        return new PageImpl<>(this.lawRegulationsStandardMesDao.queryAllByLimit(lawRegulationsStandardMes, pageRequest), pageRequest, total);
    }

    @Override
    public List<LawRegulationsStandardMes> queryAll(LawRegulationsStandardMes lawRegulationsStandardMes) {
        return lawRegulationsStandardMesDao.queryAll(lawRegulationsStandardMes);
    }

    @Override
    public List<LawRegulationsStandardMesList> queryAllV2(LawRegulationsStandardMes lawRegulationsStandardMes) {
        List<LawRegulationsStandardMesList> lawRegulationsStandardMesLists = new ArrayList<>();
        List<LawRegulationsStandardMes> lawRegulationsStandardMesList = lawRegulationsStandardMesDao.queryAll(lawRegulationsStandardMes);
        if (!CollectionUtils.isEmpty(lawRegulationsStandardMesList)) {
            for (LawRegulationsStandardMes var : lawRegulationsStandardMesList) {
                LawRegulationsStandardMesList lawRegulationsStandardMesListResp = new LawRegulationsStandardMesList();
                BeanUtils.copyBeanProp(lawRegulationsStandardMesListResp, var);
                lawRegulationsStandardMesLists.add(lawRegulationsStandardMesListResp);
            }
        }
        return lawRegulationsStandardMesLists;
    }

    /**
     * 新增数据
     *
     * @param lawRegulationsStandardMes 实例对象
     * @return 实例对象
     */
    @Override
    public LawRegulationsStandardMes insert(LawRegulationsStandardMes lawRegulationsStandardMes) {
        this.lawRegulationsStandardMesDao.insert(lawRegulationsStandardMes);
        return lawRegulationsStandardMes;
    }

    /**
     * 修改数据
     *
     * @param lawRegulationsStandardMes 实例对象
     * @return 实例对象
     */
    @Override
    public LawRegulationsStandardMes update(LawRegulationsStandardMes lawRegulationsStandardMes) {
        this.lawRegulationsStandardMesDao.update(lawRegulationsStandardMes);
        return this.queryById(lawRegulationsStandardMes.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.lawRegulationsStandardMesDao.deleteById(id) > 0;
    }

    @Override
    public String importExcel(List<LawRegulationsStandardMes> lawRegulationsStandardMess, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(lawRegulationsStandardMess) || lawRegulationsStandardMess.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        lawRegulationsStandardMesDao.insertBatch(lawRegulationsStandardMess);
        return "成功";
    }
}
